package com.hdl;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * 创建排序的Reducer
 * 第一对：K1: Text， V1: NullWritable
 * 第二对：K2: NullWritable， V2: Text
 */
public class SortReducer extends Reducer<LongWritable, LongWritable, Text, NullWritable> {
    private static long index = 0;
    private static long tmpNum = 0;

    @Override
    protected void reduce(LongWritable key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
        // 遍历值，然后进行自增，key可能会重复，只遍历key可能会出现相同数据只展示了一个
        for (LongWritable value : values) {
            if (value.get() >= tmpNum) {
                index++;
                tmpNum = value.get();
            }
            context.write(new Text(index + "\t" + key.get() ), NullWritable.get());
        }
    }
}
